home *** CD-ROM | disk | FTP | other *** search
- // Tools Plus Tutorial -- Editing Fields
-
-
-
-
- #include "ToolsPlus.h"
- #include "String.h" // ANSI C string manipulation
- #include "PascalStrHandles.c" // Quick 'n' Dirty routines for copying a C string to a
- // Pascal "String Handle" and vice versa.
-
-
- #define ApplMenu 0 // Menu constants to make code more readable…
- #define FileMenu 1
- #define EditMenu 2
-
- #define bToField 1 // "To Field" and "From Field" buttons…
- #define bFromField 2
-
-
-
- typedef unsigned char Str30[31]; // A 30-character string
-
-
-
- TPPollRecord Poll; // Polling record to retrieve event information
- Boolean ExitTheDemo; // Should the demo terminate?
-
- Handle hField1; // Handle to 1st editing field's 30-char string
- Handle hField2, hField3; // Handles to 2nd and 3rd editing fields' 255-char string
- short Field; // Field number
-
- Str255 EditString; // Field's text
- short theButton; // Button number clicked in an alert
-
-
-
- void ApplicationInitialization (void);
- void ProcessMenuSelection (void);
-
-
-
-
-
-
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void main(void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- MaxApplZone();
-
-
- if (!InitToolsPlus(1, 1, UseColor))
- ExitToShell();
-
- ApplicationInitialization();
-
- while (!ExitTheDemo) //Main Event Loop
- {
- if (PollSystem(&Poll)) //If an event is available, process the event…
- {
- switch (Poll.What)
- {
- case doMenu:
- ProcessMenuSelection();
- break;
-
-
- case doGoAway: // User clicked a window's close box
- WindowClose(Poll.Window);
- break;
-
-
- case doKeyDown: case doAutoKey: // Typing (check for Tab and Shift/Tab)…
- // If key-stroke is in window 1, it's a tab, and the Command, Option and
- // Control keys were not down…
- if ((Poll.Key.Chr == TabKey) && !(Poll.Modifiers.Bits.CmdKey || Poll.Modifiers.Bits.OptionKey || Poll.Modifiers.Bits.ControlKey))
- {
- SaveFieldString(); // Save the field's edited text as the field's string
- Field = ActiveFieldNumber(); // Determine the active field number
- if (!Poll.Modifiers.Bits.ShiftKey) // TAB: to next field…
- Field = Field + 1 - 3 * (Field == 3); // Add 1. If field=3, start at 1 again.
- else // SHIFT-TAB: to previous field…
- Field = Field - 1 + 3 * (Field == 1); // Subtract 1. If field=1, start at 3.
- ActivateField(Field, teSelectAll); // Select all the text in the newly activated field
- }
- break;
-
-
- case doClickField: // User clicked in an inactive field…
- CurrentWindow(1); // The following commands apply to window #1…
- SaveFieldString(); // Save the field's edited text as the field's string
- ClickInField(); // Process the click in the inactive field
- break;
-
-
-
- case doButton: // User clicked a button…
- switch (Poll.Button.Num)
- {
- case bToField: // Write into field #3…
- PasteIntoField(3, "\pThis is the replacement text pasted by the application", teReplace);
- break;
-
- case bFromField: // Display contents of field #3…
- if (ActiveFieldNumber() == 3) // If user is editing field #3…
- GetFieldString(EditString); // get the EDITED text.
- else
- BlockMove(*hField3, EditString, **hField3); // Copy field's (permanent) text to EditString
-
- theButton = AlertBox(noteIcon, EditString, -OkAlert);
- break;
- }
- break;
-
-
- default:
- break; //All other events are ignored
- }
- }
- }
- }
-
-
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void ApplicationInitialization (void)
- {
- // Do all the application setup before you start polling for events…
-
- #define DemoWindow1 1
-
- // Create an Apple menu with an 'About…' item…
- AppleMenu("\pAbout My App…");
-
- // Create the standard File and Edit menus…
- Menu(FileMenu, 0, enabled, "\pFile");
- Menu(FileMenu, 1, disabled, "\pNew/N");
- Menu(FileMenu, 2, disabled, "\pOpen/O");
- Menu(FileMenu, 3, disabled, "\pClose/W");
- Menu(FileMenu, 4, disabled, mDividingLine);
- Menu(FileMenu, 5, enabled, "\pQuit/Q");
-
- Menu(EditMenu, 0, enabled, "\pEdit");
- Menu(EditMenu, 1, disabled, "\pUndo/Z");
- Menu(EditMenu, 2, disabled, mDividingLine);
- Menu(EditMenu, 3, disabled, "\pCut/X");
- Menu(EditMenu, 4, disabled, "\pCopy/C");
- Menu(EditMenu, 5, disabled, "\pPaste/V");
- Menu(EditMenu, 6, disabled, "\pClear");
- Menu(EditMenu, 7, disabled, mDividingLine);
- Menu(EditMenu, 8, disabled, "\pShow Clipboard…");
- UpdateMenuBar();
-
-
- WindowOpen(DemoWindow1, 5, 40, 206, 312, "\pEditing Field Tutorial", noGrowDocProc, GoAway, NotModal);
-
- // Allocate text handles for editing fields, and initialize to a null string…
- hField1 = (Handle)NewStrHandle(30);
- hField2 = (Handle)NewStrHandle(255);
- hField3 = (Handle)NewStrHandle(255);
-
- // Set each field's text to a default value…
- Cstr2PHdl("Length limited field", hField1);
- Cstr2PHdl("Single line editing field.", hField2);
- Cstr2PHdl("This is a multiple-line editing field which incorporates word wrap.", hField3);
-
- // Create first field using Monaco 9pt. It is length limited to 30 characters…
- TextFont(monaco);
- TextSize(9);
- FieldLengthLimit(on);
- NewField(1, 10, 10, 191, 21, hField1, teBoxNoCR, teJustLeft);
- FieldLengthLimit(off);
-
- // Create second field using Geneva 9pt bold. This is a single-line field…
- TextFont(geneva);
- TextFace(bold);
- NewField(2, 10, 30, 191, 42, hField2, teBoxNoCR, teJustLeft);
-
- // Create third field using Chicago 12pt. This is a multiple line editing field…
- TextFont(systemFont);
- TextSize(12);
- TextFace(0);
- NewField(3, 10, 51, 191, 227, hField3, teBoxCR, teJustLeft);
-
- ActivateField(1, teSelectEnd); // Activate the first field with insertion point at the end of the text
-
-
- NewButton(bToField, 6, 238, 95, 258, "\pWrite to #3", pushButProc, enabled, notSelected);
- NewButton(bFromField, 105, 238, 195, 258, "\pGet from #3", pushButProc, enabled, notSelected);
- ExitTheDemo = false;
- }
-
-
-
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void ProcessMenuSelection (void)
- {
- #define NewItem 1
- #define OpenItem 2
- #define CloseItem 3
- #define QuitItem 5
-
- short theButton;
-
- switch (Poll.Menu.Num)
- {
- case ApplMenu: // Apple menu's 'About…' item
- theButton = AlertBox(NoIcon, "\pThis is my application’s about box. (Click to close)", NoButtonAlert);
- break;
-
- case FileMenu:
- switch (Poll.Menu.Item)
- {
- case NewItem:
- break;
- case OpenItem:
- break;
- case CloseItem:
- break;
- case QuitItem:
- ExitTheDemo = true;
- break;
- }
- }
-
- MenuHilite(0); // Turn off highlighted menu
- }